package com.yh.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.bean.BrowseRecords;
import com.yh.bean.News;
import com.yh.bean.NewsComment;
import com.yh.bean.NewsReply;
import com.yh.service.NewsDaoSer;

@Controller
public class NewsController {
	private int record=0;
	private String comStatus="0";
	@Autowired
	private NewsDaoSer newsDaoSer;
	@RequestMapping("/queryList")
	@ResponseBody
	public Map<String,Object> queryList(int currentPgae){
		System.out.println("进入第一个方法");
		Page page=PageHelper.startPage(currentPgae, 4);
		
		List<News> news=newsDaoSer.queryNewsSer();
		System.out.println("............................................."+news);
		int pages=page.getPages();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("pages", pages);
		map.put("news", news);
		System.out.println("返回的数据："+news);
		return map;
	
	}
	
	@RequestMapping("ManagerqueryList")
	@ResponseBody
	public Map<String,Object> ManagerqueryList(int currentPgae){
		System.out.println("进入第一个方法8888888888888888888888888888888888888");
		Page page=PageHelper.startPage(currentPgae, 4);
		
		List<News> news=newsDaoSer.queryNewsSer();
		System.out.println(".............................................++++++++++"+news);
		int pages=page.getPages();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("pages", pages);
		map.put("news", news);
		System.out.println("返回的数据："+news);
		return map;
	
	}
	
	
	
	
	@RequestMapping("/newsDetails")
	public String queryNewsDetails(HttpSession session,String newsId){
		System.out.println("进入查询新闻详情的方法"+"获取到的新闻ID："+newsId);
		if(newsId!=""|newsId!=null){
			
			int news=Integer.parseInt(newsId);
			News newss=newsDaoSer.queryNewsDetailss(news);
			int updateNewss=newsDaoSer.updateNews(news);
			session.setAttribute("news", newss);
		}
		
		
		return "lhtml/NewsDetails";
	}
	
	@RequestMapping("LeaveMessage.do")
	public String leaveMessage(NewsComment newsC,String news_Id,String user_Id){
		System.out.println("进入留言的方法---------------");
		if(news_Id!="" | news_Id!=null & user_Id!="" | user_Id!=null){
			int newsIdd=Integer.parseInt(news_Id);
			int user_Idd=Integer.parseInt(user_Id);
			newsC.setNews_Id(newsIdd);
			newsC.setUser_Id(user_Idd);
			int leaveMe=newsDaoSer.leaveM(newsC);
		}
		
		return "lhtml/NewsDetails";
	}
	@RequestMapping("queryComment")
	@ResponseBody
	public List<NewsComment> queryC(String news_Id){
		
		System.out.println("******************进入查询评论的方法*******************");
		if(news_Id!=""|news_Id!=null){
			int news=Integer.parseInt(news_Id);
			List<NewsComment> newss=newsDaoSer.queryComm(news);
			System.out.println("返回的数据------------------------------------------------："+newss);
			return newss;
		}else{
			return null;
		}
	}
	
	
	@RequestMapping("replyComment.do")
	public String replyCom(NewsReply newsR,String comId,String userId){
	System.out.println("*$$$$$$$$$$$$$$$$$$进入回复评论的方法$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
	if(comId!="" | comId!=null & userId!="" |userId!=null){
		int comIdd=Integer.parseInt(comId);
		int userIdd=Integer.parseInt(userId);
		newsR.setComId(comIdd);
		newsR.setUserId(userIdd);
		System.out.println("获取从前台输入的值："+newsR);
		int replyC=newsDaoSer.replyComm(newsR);
	}
	
	return "lhtml/NewsDetails";
	}
	
	 
	@RequestMapping("/ShowReply")
	@ResponseBody
	public List<NewsReply> queryR(String comId){
		System.out.println("*$+++++++++++++++++++++++++++进入查询的回复内容的方法+++++++++++++++++++++++++++"+comId);
		if(comId!="" | comId!=null){
			int comIdd=Integer.parseInt(comId);
			List<NewsReply> queryReply=newsDaoSer.queryReply(comIdd);
			System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++"+queryReply);
			return queryReply;
		}
		
		
		return null;
	}
	
	
	
	@RequestMapping("addNewss")
	public String addNews(News news,String user_Id,MultipartFile photos,HttpSession session,HttpServletRequest request){
		System.out.println("*$+++++++++++++++++++++++++++进入增加新闻的方法---------------------------------------------------------------------"+user_Id+photos);
	if(user_Id!="" |user_Id !=null & !photos.isEmpty()){
		int user_ID=Integer.parseInt(user_Id);
		news.setUser_Id(user_ID);
		Date data=new Date();
		SimpleDateFormat spl=new SimpleDateFormat("yyyy-MM-dd");
		String time=spl.format(data);
		news.setTime(time);
		String filePath=request.getSession().getServletContext().getRealPath("/photo");
		String uploadPath=filePath+"/"+photos.getOriginalFilename();
		
		try {
			IOUtils.copy(photos.getInputStream(), new FileOutputStream(new File(uploadPath)));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("文件的位置："+filePath);
		System.out.println("文件表单名称："+photos.getName());
		System.out.println("文件的名称："+photos.getOriginalFilename()); 
		news.setPhoto("photo/"+photos.getOriginalFilename());
		news.setStatistics(0);
		news.setComStatus(comStatus);
		int addNewsss=newsDaoSer.addNewss(news);
		
	}
	return "../news";
		
	}
	@RequestMapping("deleteNews")
	public String deleteNNewss(String newsId){
		System.out.println("进ru新闻删除的方法+++++++++++++++++++++++-------------------------");
		if(newsId!="" | newsId!=null){
			int newsIdd=Integer.parseInt(newsId);
			int deleteN=newsDaoSer.deleteNewss(newsIdd);
		
			return "/news";
		}
		return null;
		
	}
	
	
	//-----------=------------====================================================================================
/*	@RequestMapping("")
	public String createBrowseR(BrowseRecords b,String g_id,String u_id){
		if(g_id!="" | g_id!=null & u_id!="" | u_id!=null){
			int g_Idd=Integer.parseInt(g_id);
			int u_Idd=Integer.parseInt(u_id);
			Date date=new Date();
			SimpleDateFormat sql=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String brTime=sql.format(date);
			b.setG_id(g_Idd);
			b.setU_id(u_Idd);
			b.setBrTime(brTime);
			int createBrowerRecords=newsDaoSer.createBrowserRecords(b);
			
		}
		
		
		
		return null;
		
		
	}*/
}